/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 27/09/2004
 *
 */

package au.com.lastweekend.jim.io.exif;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

/**
 * @author grant@lastweekend.com.au
 * @version $Id: ExifTransform.java,v 1.2 2005/06/09 11:15:37 ggardner Exp $
 */
public interface ExifTransform {

    ExifTransform NO_TRANSFORM = new NoTransform();
    ExifTransform ROTATE_CLOCKWISE = new RotateClockwise();
    ExifTransform ROTATE_ANTI_CLOCKWISE = new RotateAntiClockwise();

    /**
     * 
     * @return Should X/Y co-ordinates be transposed
     * 
     */
    boolean isXYTransposed();

    boolean isThumbnailTransformRequired();

    BufferedImage transformThumbnail(BufferedImage originalImage);

    /** Null transform */
    public final class NoTransform implements ExifTransform {

        private NoTransform() {

            super();
        }

        public boolean isXYTransposed() {

            return false;
        }

        /*
         * @see au.com.lastweekend.jim.io.exif.ExifTransform#isThumbnailTransformRequired()
         */
        public boolean isThumbnailTransformRequired() {

            return false;
        }

        /*
         * @see au.com.lastweekend.jim.io.exif.ExifTransform#transformThumbnail(java.awt.Image)
         */
        public BufferedImage transformThumbnail(BufferedImage originalImage) {

            return originalImage;
        }

    }

    /** abstract rotation transform */
    public abstract class RotateTransform implements ExifTransform {

        public abstract boolean isXYTransposed();

        public abstract boolean isThumbnailTransformRequired();

        public BufferedImage transformThumbnail(BufferedImage originalImage) {

            AffineTransform at = new AffineTransform();
            makeTransform(at, originalImage);

            AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            BufferedImage destImage = new BufferedImage(originalImage.getHeight(), originalImage.getWidth(), originalImage
                    .getType());
            return op.filter(originalImage, destImage);
        }

        protected abstract void makeTransform(AffineTransform at, BufferedImage originalImage);
    }

    /** RotateClockwise */
    final class RotateClockwise extends RotateTransform {

        private RotateClockwise() {

            super();
        }

        @Override
        protected void makeTransform(AffineTransform at, BufferedImage originalImage) {

            at.rotate(Math.toRadians(90));
            at.translate(0, -originalImage.getHeight());

        }

        @Override
        public boolean isThumbnailTransformRequired() {

            return true;
        }

        @Override
        public boolean isXYTransposed() {

            return true;
        }

    }

    /** RotateAntiClockwise */
    final class RotateAntiClockwise extends RotateTransform {

        private RotateAntiClockwise() {

            super();
        }

        @Override
        protected void makeTransform(AffineTransform at, BufferedImage originalImage) {

            at.rotate(Math.toRadians(-90));
            at.translate(-originalImage.getWidth(), 0);

        }

        @Override
        public boolean isThumbnailTransformRequired() {

            return true;
        }

        @Override
        public boolean isXYTransposed() {

            return true;
        }
    }

}